<script setup lang="tsx">
import { ElIcon, ElUpload } from 'element-plus'
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, watch, ref } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { useI18n } from '@/hooks/web/useI18n'
import { uploadImg } from '@/api/api'

const { t } = useI18n()

const { required } = useValidator()

const { formRegister, formMethods } = useForm()
const { setValues, getFormData, getElFormExpose } = formMethods

const props = defineProps({
  currentRow: {
    type: Object as PropType<any>,
    default: () => null
  }
})

const formSchema = ref<FormSchema[]>([
  {
    field: 'username',
    label: '用户名',
    component: 'Input',
    colProps: {
      span: 12
    }
  },
  {
    field: 'phone',
    label: '手机号',
    component: 'Input',
    colProps: {
      span: 12
    }
  },
  {
    field: 'password',
    label: '密码',
    component: 'Input',
    colProps: {
      span: 12
    }
  },
  {
    field: 'status',
    label: t('active.status'),
    component: 'Select',
    componentProps: {
      options: [
        {
          label: t('userDemo.enable'),
          value: 1
        },
        {
          label: t('userDemo.disable'),
          value: 0
        }
      ]
    },
    colProps: {
      span: 12
    }
  },
  {
    field: 'avatar_url',
    label: '头像',
    formItemProps: {
      slots: {
        default: (data) => {
          return (
            <>
              <ElUpload
                action="/api/upload/file"
                show-file-list={false}
                onSuccess={(res) => setValues({ avatar_url: res.data.url })}
              >
                {data.avatar_url ? <img src={data.avatar_url} class="avatar" /> : null}
                {!data.avatar_url ? (
                  <ElIcon class="avatar-uploader-icon" size="large">
                    上传
                  </ElIcon>
                ) : null}
              </ElUpload>
            </>
          )
        }
      }
    }
  }
])

const rules = reactive({
  username: [required()],
  phone: [required()],
  password: [required()],
  status: [required()]
})

const submit = async () => {
  const elForm = await getElFormExpose()
  const valid = await elForm?.validate().catch((err) => {
    console.log(err)
  })
  if (valid) {
    const formData = await getFormData()
    console.log(formData)
    return formData
  }
}

watch(
  () => props.currentRow,
  (currentRow) => {
    if (!currentRow) return
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  submit
})
</script>

<template>
  <Form label-position="top" :rules="rules" @register="formRegister" :schema="formSchema" />
</template>
<style lang="less">
.el-upload {
  position: relative;
  overflow: hidden;
  cursor: pointer;
  border: 1px dashed var(--el-color-primary);
  border-radius: 6px;
  transition: var(--el-transition-duration-fast);
}

.el-upload:hover {
  border-color: var(--el-color-primary);
}
.avatar {
  width: 180px;
  height: 180px;
}
.el-icon.avatar-uploader-icon {
  width: 180px;
  height: 180px;
  font-size: 28px;
  color: #8c939d;
  text-align: center;
}
</style>
